package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResOrderEntity;
import com.taomu.app.zongchouhuli.http.res.ResOrderListEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderListAdp extends RecyclerView.Adapter<OrderListAdp.OrderListHolder>{

    private List<ResOrderListEntity.DataBean> mData;

    private LayoutInflater inflater;

    private Context mContext;

    public OrderListAdp(Context mContext){
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.mData = new ArrayList<>();
    }

    public void setData(List<ResOrderListEntity.DataBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResOrderListEntity.DataBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public OrderListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view  = inflater.inflate(R.layout.item_order,parent,false);
        return new OrderListHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderListHolder holder, int position) {
        final ResOrderListEntity.DataBean entity = mData.get(position);
        holder.update(entity);
        holder.mRechargeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onRechagerClick(entity);
                }
            }
        });
        holder.mDeleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onDelteClick(entity);
                }
            }
        });
        holder.mPayTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onPayClick(entity);
                }
            }
        });
        holder.mConfirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onConfrimClick(entity);
                }
            }
        });
        holder.mCancleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onCancleClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class OrderListHolder extends RecyclerView.ViewHolder {

        private TextView mOrderNumTv;

        private TextView mStateTv;

        private TextView mPriceTv;

        private ImageView mIconIm;

        private TextView mCancleTv;

        private TextView mConfirmTv;

        private TextView mDeleteTv;

        private TextView mPayTv;

        private TextView mRechargeTv;

        private TextView mOrderTv;

        private TextView mOrderTimeTv;

        public OrderListHolder(View itemView) {
            super(itemView);
            mOrderTv = itemView.findViewById(R.id.order_name);
            mOrderNumTv = itemView.findViewById(R.id.item_order_num_tv);
            mStateTv = itemView.findViewById(R.id.item_order_state_tv);
            mPriceTv = itemView.findViewById(R.id.item_order_price_tv);
            mIconIm = itemView.findViewById(R.id.item_order_icon_im);
            mCancleTv = itemView.findViewById(R.id.item_order_cancle_tv);
            mConfirmTv = itemView.findViewById(R.id.item_order_confirm_tv);
            mDeleteTv = itemView.findViewById(R.id.item_order_delete_tv);
            mPayTv = itemView.findViewById(R.id.item_order_pay_tv);
            mRechargeTv = itemView.findViewById(R.id.item_order_recharge_tv);
            mOrderTimeTv = itemView.findViewById(R.id.item_order_time);
        }

        public void update(ResOrderListEntity.DataBean entity){
            if(!TextUtils.isEmpty(entity.getCreate_date())) {
                mOrderTimeTv.setText(TimeUtils.timeFomart(Long.valueOf(entity.getCreate_date())));
            }
            mOrderNumTv.setText("订单编号："+entity.getOrder_id());


            mPriceTv.setText("¥"+entity.getTotal_price());
            if("order_complete".equals(entity.getOrder_status_id())) {
                mStateTv.setText("已完成");
            }else if("order_wait".equals(entity.getOrder_status_id())){
                mStateTv.setText("待支付");
                if(entity.getQuantity() != 0) {
                    mPriceTv.setText("¥" + entity.getTotal_price() + "    X" + String.valueOf(entity.getQuantity()));
                }
            }else if("order_wait_confirm".equals(entity.getOrder_status_id())){
                mStateTv.setText("审核中");
            }else if("order_cancel".equals(entity.getOrder_status_id())){
                mStateTv.setText("已取消");
            }
            showMenu(entity.getOrder_status_id(),entity.getOrder_type_id());
            if("order_recharge".equals(entity.getOrder_type_id())){
                Glide.with(mContext).load(R.drawable.crash_recharge_im).into(mIconIm);
                mOrderTv.setText("充值");
            }else if("order_forward".equals(entity.getOrder_type_id())){
                Glide.with(mContext).load(R.drawable.crash_recharge_im).into(mIconIm);
                mOrderTv.setText("提现");
            }else {
                Glide.with(mContext).load(entity.getImg_uri()).into(mIconIm);
                mOrderTv.setText(entity.getName());
                mRechargeTv.setVisibility(View.GONE);
            }
            if(TextUtils.isEmpty(entity.getTotal_price())){
                mPriceTv.setText("¥0");
            }
            if("null".equals(entity.getTotal_price())){
                mPriceTv.setText("¥0");
            }
        }

        public void showMenu(String typeid,String status){
            if("order_wait".equals(typeid)){
                //待付款-确认付款
                mCancleTv.setVisibility(View.VISIBLE);
                if("order_recharge".equals(status)){
                    mConfirmTv.setVisibility(View.VISIBLE);
                    mPayTv.setVisibility(View.GONE);
                }else if("order_forward".equals(status)){
                    mConfirmTv.setVisibility(View.VISIBLE);
                    mPayTv.setVisibility(View.GONE);
                }else {
                    mPayTv.setVisibility(View.VISIBLE);
                    mConfirmTv.setVisibility(View.GONE);
                }
                mDeleteTv.setVisibility(View.GONE);
                mRechargeTv.setVisibility(View.GONE);
            }else if("order_wait_confirm".equals(typeid)){
                //审核中
                mCancleTv.setVisibility(View.VISIBLE);
                mConfirmTv.setVisibility(View.GONE);
                mPayTv.setVisibility(View.GONE);
                mDeleteTv.setVisibility(View.GONE);
                mRechargeTv.setVisibility(View.GONE);
            }else if("order_complete".equals(typeid)){
                //已完成
                mCancleTv.setVisibility(View.GONE);
                mConfirmTv.setVisibility(View.GONE);
                mPayTv.setVisibility(View.GONE);
                mDeleteTv.setVisibility(View.VISIBLE);
                mRechargeTv.setVisibility(View.VISIBLE);
                if("order_recharge".equals(status)){
                    mRechargeTv.setVisibility(View.VISIBLE);
                    mRechargeTv.setText("再次充值");
                }else if("order_forward".equals(status)){
                    mRechargeTv.setVisibility(View.VISIBLE);
                    mRechargeTv.setText("再次提现");
                }else {
                    mRechargeTv.setVisibility(View.GONE);
                }
            }else if("order_cancel".equals(typeid)){
                //取消
                mCancleTv.setVisibility(View.GONE);
                mConfirmTv.setVisibility(View.GONE);
                mPayTv.setVisibility(View.GONE);
                mDeleteTv.setVisibility(View.GONE);
                mRechargeTv.setVisibility(View.GONE);
            }
        }
    }

    public OrderListListener listener;

    public void setOrderListListener(OrderListListener listener){
        this.listener = listener;
    }

    public interface OrderListListener{
        void onRechagerClick(ResOrderListEntity.DataBean entity);

        void onConfrimClick(ResOrderListEntity.DataBean entity);

        void onDelteClick(ResOrderListEntity.DataBean entity);

        void onCancleClick(ResOrderListEntity.DataBean entity);

        void onPayClick(ResOrderListEntity.DataBean entity);
    }
}

